﻿package bdz.common.utils {

	public class MathUtils {
		
		public static function getProximity(point1, point2) {
			var pt1 = (point1.x - point2.x) * (point1.x - point2.x);
			var pt2 = (point1.y - point2.y) * (point1.y - point2.y);
			return Math.sqrt(pt1 + pt2);
		}
		public static function getAngle(point, origin, radian:Boolean) {
			if (!radian) {
				return Math.atan2(point.y - origin.y,point.x - origin.x) * 180 / Math.PI;
			}
			return Math.atan2(point.y - origin.y,point.x - origin.x);
		}
		public static function degreeToRadian(deg:Number):Number {
			return deg * Math.PI / 180;
		}
		public static function radianToDegree(rad:Number):Number {
			return rad * 180 / Math.PI;
		}
		public static function convertRotation2Pos(angle, radius, origin) {
			origin = (origin == undefined) ? 0 : origin;
			return {x:origin + Math.acos(Math.tan(angle)) / radius,y:origin + Math.asin(Math.tan(angle)) / radius};
		}
		public static function positionFromRadius(angle, radius, prop, origin) {
			origin = (origin == undefined) ? {x:0, y:0} : origin;
			return prop == "x"?origin.x + Math.cos(angle) * radius:origin.y + Math.sin(angle) * radius;
		}
		public static function convertTimeToAngles(tdate:Date = null) {
			var dte:Date = tdate || new Date();
			return {hours:dte.getHours() - 12 + dte.getMinutes() / 60 * 30 - 90,mins:dte.getMinutes() * 6 - 90,secs:dte.getSeconds() * 6 - 90};
		}
		public static function generateRandomPointArea(xmin, xmax, ymin, ymax) {
			var rx = Math.random()*(xmax-xmin)+xmax;
			var ry = Math.random()*(ymax-ymin)+ymax;
			return {x:rx,y:ry};
		}
		public static function getHypotenuse(x, y) {
			return Math.sqrt(x * x + y * y);
		}
		public static function get3DscaleRatio(focalPoint, zindex) {
			return focalPoint / focalPoint + zindex;
		}
		public static function generateRandomPoint(base) {
			if (base) {
				var rx = Math.random()*base.width;
				var ry = Math.random()*base.height;
			}
			return {x:rx,y:ry};
		}
		public static function scaleRatioY(sy:Number, tx:Number, ty:Number):Object{
			return {width:((sy/ty)*tx),height:sy};
		}
		public static function scaleRatioX(sx:Number, tx:Number, ty:Number):Object{
			return {width:sx,height:((sx/tx)*ty)};
		}
		public static function decimalToBinary(iNumber, len) {
			var bin = "";
			var oNumber = iNumber;
			while (iNumber>0) {
				if (iNumber%2) {
					bin = "1"+bin;
				} else {
					bin = "0"+bin;
				}
				iNumber = Math.floor(iNumber/2);
			}
			while (bin.length<len) {
				bin = "0"+bin;
			}
			return bin;
		}
		public static function randomPercentChance(n:Number):Boolean{
			if(Math.floor(Math.random()*100) < n){
				return true;
			}
			return false;
		}
		public static function convertToStringCurrency(num:Number,decimalPlace:Number=2,currency:String="$"):String {

			var neg:Boolean = Boolean(num < 0);
		
		
			num=Math.abs(num);
		
			var roundedAmount:String=String(num.toFixed(decimalPlace));
		
		
			var amountArray:Array=roundedAmount.split(".");
			var dollars:String=amountArray[0];
			var cents:String=amountArray[1];
			//create dollar amount
			var dollarFinal:String="";
			var i:int=0;
			for (i; i < dollars.length; i++) {
				if (i > 0 && (i % 3 == 0 )) {
					dollarFinal=","+dollarFinal;
				}
		
				dollarFinal=dollars.substr(- i-1,1)+dollarFinal;
			}
		
			
			var centsFinal:String=String(cents);
		
			var missingZeros:int=decimalPlace-centsFinal.length;
		
			if (centsFinal.length<decimalPlace) {
				for (var j:int = 0; j < missingZeros; j++) {
					centsFinal+="0";
				}
			}
		
			var finalString:String="";
		
			if (neg) {
				finalString="-"+currency+dollarFinal;
			} else {
				finalString=currency+dollarFinal;
			}
		
			if (decimalPlace>0) {
				finalString+="."+centsFinal;
			}
		
			return finalString;
		}
		public static function bitPositiveRound(n:Number):Number{
			return n|0;
		}
		/**
		 * USEFUL STUFF
		 *
		 * num|0 = floor positive or round
		 * num>>1 = divide by 2
		 * count&1 = alternation
		 */
		
	}
}